/**
 * 20220712:rewrite with typescript
 */
import WorkSpace from './work-space';
import { WebGLRenderer } from 'three';
import {SceneManager} from './scene-manager';
import {EventConfig, ShiftSceneEventObject, UiOperateEventObject} from "three-lib/container/event-config";
import {Event} from 'three-lib/container/event';


/**
 * base class for work space manager
 */
export default abstract class SpaceCenterBase{

    protected spaces:Array<WorkSpace>;
    protected renderer:WebGLRenderer;
    protected sceneManager:SceneManager;
    protected eventCenter:Event;
    protected curSpace:WorkSpace|null=null;
    protected preSpace:WorkSpace|null=null;

    constructor(renderer:WebGLRenderer,sceneManager:SceneManager,eventCenter:Event){
        this.sceneManager=sceneManager;
        this.renderer=renderer;
        this.eventCenter=eventCenter;
        this.initCommonComponents();
        this.spaces=this.initSpaces();
        for(let space of this.spaces){
            this.sceneManager.addToQueue(space);
        }
        this.distributeEvent();
        this.initialize();
    }
    
    protected abstract initCommonComponents():void;
    protected abstract initSpaces():Array<WorkSpace>;
    protected abstract initialize():void;

    private distributeEvent():void{
        this.eventCenter.addEventListener(EventConfig.resetScene,this.resetSceneHandle.bind(this));
        this.eventCenter.addEventListener(EventConfig.resetSight,this.resetSightHandle.bind(this));
        this.eventCenter.addEventListener(EventConfig.shiftScene,this.shiftSceneHandle.bind(this));
        this.eventCenter.addEventListener(EventConfig.uiOperate,this.uiOperateHandle.bind(this));
    }

    private shiftSceneHandle(e:ShiftSceneEventObject){
        let cur=this.spaces[e.paras!.current]||null;
        let pre=this.spaces[e.paras!.previous]||null;
        if(cur===null)return;
        this.onBeforeSpaceShift(e.paras.current,e.paras.previous);
        this.sceneManager.renderId=cur.uuid;
        this.curSpace=cur;
        this.preSpace=pre;
        if(pre){
            pre.active=false;
        }
        cur.active=true;
        this.onAfterSpaceShift(e.paras.current,e.paras.previous);
    }
    protected onBeforeSpaceShift(cur:number,pre:number):void{}
    protected onAfterSpaceShift(cur:number,pre:number):void{}
    protected abstract resetSightHandle():void;
    protected abstract resetSceneHandle():void;
    protected abstract uiOperateHandle(e:UiOperateEventObject<any>):void;
 
}
